package com.jdk.parallerutils.countdownlatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description :等待多线程完成的CountDownLatch
 * @FileName: CountDownLatchTest.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月18日 上午9:28:07
 * @Version:V1.00
 */
public class ThreadJoinTest {

	// 模拟了100米赛跑，10名选手已经准备就绪，只等裁判一声令下。当所有人都到达终点时，比赛结束。
	public static void main(String[] args) throws InterruptedException {

		GameThread gameThread = new GameThread();

		List<PlayerThread> pList = new ArrayList<PlayerThread>();
		for (int index = 0; index < 10; index++) {
			PlayerThread thread = new PlayerThread(index + 1);
			pList.add(thread);
		}

		for (PlayerThread playerThread : pList) {
			playerThread.start();
		}
		
		gameThread.start();
		gameThread.join();
		
		for (PlayerThread playerThread : pList) {
			playerThread.join();
		}
		System.out.println("Game Over");
	}
}

class PlayerThread extends Thread {

	private int no;

	PlayerThread(int no) {
		this.no = no;
	}

	@Override
	public void run() {
		try {
			TimeUnit.SECONDS.sleep(1);
			System.out.println("No." + no + " arrived");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class GameThread extends Thread {

	@Override
	public void run() {
		System.out.println("Game Start");
	}
}
